package com.bjgzy.courselive.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerCourseLiveMainComponent;
import com.bjgzy.courselive.mvp.contract.CourseLiveMainContract;
import com.bjgzy.courselive.mvp.presenter.CourseLiveMainPresenter;
import com.bjgzy.courselive.mvp.ui.fragment.CourseLiveHomeFragment;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.imgaEngine.GlideImageLoader;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.eduhzy.ttw.commonsdk.core.Constants.LIVE_IDS;
import static com.eduhzy.ttw.commonsdk.core.Constants.LIVE_TITLES;
import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-30 15:34:09
 */
@Route(path = RouterHub.COURSELIVE_COURSELIVEMAINACTIVITY)
public class CourseLiveMainActivity extends BaseActivity<CourseLiveMainPresenter> implements CourseLiveMainContract.View {

    @BindView(R2.id.banner)
    Banner banner;
    @BindView(R2.id.tabs)
    TabLayout tabs;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;
    @BindView(R2.id.public_toolbar_iv_right)
    ImageView ivRight;
    @BindView(R2.id.public_toolbar_right_iv_layout)
    RelativeLayout rightLayout;



    List<Fragment> mFragments;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerCourseLiveMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.courselive_activity_home;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_course_live");
        rightLayout.setVisibility(View.VISIBLE);
        ivRight.setImageDrawable(getResources().getDrawable(R.drawable.courselive_mine_course_selector));
//        ivRight.setSelected(true);
        initBanner();
        mFragments = new ArrayList<>();
        for (int i = 0; i < LIVE_TITLES.length; i++) {
            CourseLiveHomeFragment instance = CourseLiveHomeFragment.newInstance(LIVE_IDS[i]);
            instance.setData(LIVE_TITLES[i]);
            mFragments.add(instance);
        }
        // 第二步：为ViewPager设置适配器
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getSupportFragmentManager(), Arrays.asList(LIVE_TITLES), mFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(LIVE_TITLES.length);
        // 第三步：将ViewPager与TableLayout 绑定在一起
        tabs.setupWithViewPager(viewpager);


    }

    private void initBanner() {
        List<String> list = new ArrayList<>();
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                list.add("http://bpic.ooopic.com/15/25/47/15254782-29fc73ebc655d2911a0f8d0834d7aa21.jpg");
                list.add("http://imgsrc.baidu.com/imgad/pic/item/b812c8fcc3cec3fd5ae98974dc88d43f869427df.jpg");
                list.add("http://images.rednet.cn/articleimage/2014/04/28/1400038234.jpg");
                list.add("http://img3.duitang.com/uploads/item/201605/16/20160516182054_QYy3d.thumb.700_0.png");
                list.add("http://p1.wmpic.me/article/2015/06/04/1433395467_nPszBtoR.jpg");
                list.add("http://image03.71.net/image03/16/16/71/48/d8fced20-9a46-45c0-9fd3-dff607696da0.jpg");
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String url) {
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                banner.setImages(list)
                        .setImageLoader(new GlideImageLoader())
                        .setOnBannerListener(position ->showMessage("暂未开放此功能"))
                        .start();
            }
        });


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }


    @OnClick(R2.id.public_toolbar_right_iv_layout)
    public void onViewClicked() {
        Utils.navigationWithTitle(RouterHub.COURSELIVE_MYCOURSEACTIVITY, Constants.MYCOURSE);
    }
}
